package com.fuhe.chen.vendingmachine.common;

import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class HtmlCommons {


    /**
     * 根据数字返回订单状态
     * @param status
     * @return
     */
    public String getOrderStatus(Integer status){
        if (status==null){
            return null;
        }
        switch (status){
            case 0:
                return "未支付";
            case 1:
                return "已支付";
            case 2:
                return "订单失效";
            case 3:
                return "订单退款";
        }
        return null;
    }

    /**
     * 根据数字返回订单支付方式
     * @param status
     * @return
     */
    public String getOrderPayMethod(Integer status){
        if (status==null){
            return null;
        }
        switch (status){
            case 0:
                return "支付宝";
            case 1:
                return "微信";
        }
        return null;
    }

    /**
     * 根据数字返回商品状态
     * @param status
     * @return
     */
    public String getCommodityStatus(Integer status){
        if (status==null){
            return null;
        }
        switch (status){
            case 0:
                return "正常";
            case 1:
                return "已下架";
        }
        return null;
    }

    /**
     * 根据数字返回商品状态
     * @param status
     * @return
     */
    public String getMachineStock(Integer status){
        if (status==null){
            return null;
        }
        switch (status){
            case 0:
                return "正常";
            case 1:
                return "缺货";
        }
        return null;
    }

    /**
     * 根据数字返回商品状态
     * @param status
     * @return
     */
    public String getMachineStatus(Integer status){
        if (status==null){
            return null;
        }
        switch (status){
            case 0:
                return "正常";
            case 1:
                return "停止售货";
        }
        return null;
    }

    /**
     * 根据数字返回广告状态
     * @param status
     * @return
     */
    public String getAdvertisementStatus(Integer status){
        if (status==null){
            return null;
        }
        switch (status){
            case 0:
                return "投放中";
            case 1:
                return "已下架";
        }
        return null;
    }

    /**
     * 返回标准格式时间
     * @param time
     * @return
     */
    public String getFormatTime(Long time){
        if (time==null){
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(time);
        return sdf.format(date);
    }

    /**
     * 毫秒时间
     * Long类型时间转换成时长
     */
    public static String getFormatLong(Long time){
        if(time == null){
            return null;
        }else{
            Date date = new Date(time);
            long day = time/(60*60*1000*24);
            long hour = (time - day*60*60*1000*24)/(60*60*1000);
            long minute = (time - day*60*60*1000*24- hour*60*60*1000)/(60*1000);
            return day+"天"+hour+"小时"+minute+"分";
        }
    }

    /**
     * 计算广告投放总时长
     * = 历史投放时长+ (当前时间 - 本次开始投放时间)
     */
    public static String getCompleteTime(Long usage_time , Long begin_time){
        if(begin_time == null){
            return null;
        }
        if(usage_time == null){
            usage_time = 0L;
        }
            Long complete_time = usage_time +(System.currentTimeMillis() - begin_time);
            return getFormatLong(complete_time);
    }

}
